package com.gjw.myapp.test;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.graphics.Typeface;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;

import com.gjw.myapp.R;
import com.gjw.myapp.utils.PermissionsChecker;

/**
 * author :  Administrator
 * date   :  2021/7/11  22:54
 * e-mail :  JimvenGao@163.com
 * version:  1.0
 * >>>>>>
 * desc   :
 */
public class FontActivity extends Activity {
    private static String path = Environment.getExternalStorageDirectory() + "/Android/font/fzyc.ttf";
    private String paths = "font/fzyc.ttf";
    /**
     * 权限
     */
    static final String[] PERMISSIONS = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,

    };
    private static final int PERMISSIONS_REQUEST_STORAGE = 1;
    private PermissionsChecker mPermissionsChecker;
    private TextView mTv_font;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_font);
        mTv_font = findViewById(R.id.tv_font);
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {

            if (mPermissionsChecker == null) {
                Log.d("gjw", "权限为空");
                mPermissionsChecker = new PermissionsChecker(this);
            }
            //获取读取和写入SD卡的权限
            if (mPermissionsChecker.lacksPermissions(PERMISSIONS)) {
                Log.d("gjw", "检查权限");
                //请求权限
                ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSIONS_REQUEST_STORAGE);
            } else {
                //上次已经获取到权限
                setFont();

            }
        }
    }

    /**
     * 权限同意之后的回调
     *
     * @param requestCode
     * @param permissions
     * @param grantResults
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case PERMISSIONS_REQUEST_STORAGE: {
                // 如果取消权限，则返回的值为0
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    //同意权限
                    setFont();
                } else {
                    // finish();//拒绝退出应用
                }
                return;
            }
        }
    }

    private void setFont() {
       /* Typeface typeface = Typeface.createFromFile(path);
        mTv_font.setTypeface(typeface);*/
        Typeface typeface = Typeface.createFromAsset(getAssets(), paths);
        mTv_font.setTypeface(typeface);
    }
}

